package com.example.node.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.node.R;

public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView mBottomNavigationView;
    private SelectFragment selectFragment;
    private MusicFragment musicFragment;
    private MovieFragment movieFragment;
    private MeFragment meFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        initView(view);
        return view;
    }


    private void initView(View view) {
        mBottomNavigationView = view.findViewById(R.id.mBottomNavigationView);
        //设置监听
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        //去掉渲染
        mBottomNavigationView.setItemIconTintList(null);
        //显示默认的Fragment
        showSelectFragment();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.menu_select:
                showSelectFragment();
                break;

            case R.id.menu_music:
                showMusicFragment();
                break;

            case R.id.menu_moive:
                showMovieFragment();
                break;

            case R.id.menu_me:
                showMeFragment();
                break;
        }
        return true;
    }

    //切换SelectFragment
    private void showSelectFragment(){
        FragmentManager fm=getActivity().getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        if(selectFragment==null){
            selectFragment=new SelectFragment();
            ft.add(R.id.home_content_layout,selectFragment);
        }
        hideAllFragment(ft);
        ft.show(selectFragment);
        ft.commit();
    }
    //切换MusicFragment
    private void showMusicFragment(){
        FragmentManager fm=getActivity().getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        if(musicFragment==null){
            musicFragment=new MusicFragment();
            ft.add(R.id.home_content_layout,musicFragment);
        }
        hideAllFragment(ft);
        ft.show(musicFragment);
        ft.commit();
    }
    //切换MovieFragment
    private void showMovieFragment(){
        FragmentManager fm=getActivity().getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        if(movieFragment==null){
            movieFragment=new MovieFragment();
            ft.add(R.id.home_content_layout,movieFragment);
        }
        hideAllFragment(ft);
        ft.show(movieFragment);
        ft.commit();
    }
    //切换MeFragment
    private void showMeFragment(){
        FragmentManager fm=getActivity().getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        if(meFragment==null){
            meFragment=new MeFragment();
            ft.add(R.id.home_content_layout,meFragment);
        }
        hideAllFragment(ft);
        ft.show(meFragment);
        ft.commit();
    }

    private void hideAllFragment(FragmentTransaction ft){
        if(selectFragment!=null){
            ft.hide(selectFragment);
        }
        if(musicFragment!=null){
            ft.hide(musicFragment);
        }
        if(movieFragment!=null){
            ft.hide(movieFragment);
        }
        if(meFragment!=null){
            ft.hide(meFragment);
        }
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
        if (selectFragment==null&&childFragment instanceof SelectFragment){
            selectFragment= (SelectFragment) childFragment;
        }else if (musicFragment==null&&childFragment instanceof MusicFragment){
            musicFragment= (MusicFragment) childFragment;
        }else if (movieFragment==null&&childFragment instanceof MovieFragment){
            movieFragment= (MovieFragment) childFragment;
        }else if (meFragment==null&&childFragment instanceof MeFragment){
            meFragment= (MeFragment) childFragment;
        }
    }
}
